

/*
* Copyright @ 2017 com.iflysse.trains
* crm 下午4:02:34
* All right reserved.
*
*/
	
package com.qf.dao.daoImpl;

import java.util.List;

import org.springframework.orm.hibernate5.HibernateTemplate;
import org.springframework.transaction.annotation.Transactional;

import com.qf.dao.CrmDepartmentDao;
import com.qf.pojo.CrmDepartment;
import com.qf.pojo.PageBean;

/**
* @desc: crm
* @descripe:XXXX
* @author: 周锐
* @createTime: 2017年7月24日 下午4:02:34
* @history:
* @version: v1.0

*/
@Transactional(value="txManager")
public class CrmDepartmentDaoImpl implements CrmDepartmentDao {
     
	private HibernateTemplate htl;
	
	/*
	*(non-Javadoc)
	* @see com.qf.dao.CrmDepartmentDao#addDepartment(com.qf.pojo.CrmDepartment)
	*/

	
	/**
	* @param htl the htl to set
	*/
	
	public void setHtl(HibernateTemplate htl) {
	
		this.htl = htl;
	}

	@Override
	public void addDepartment(CrmDepartment department) {

		htl.save(department);

	}

	/*
	*(non-Javadoc)
	* @see com.qf.dao.CrmDepartmentDao#updateDepartment(com.qf.pojo.CrmDepartment)
	*/

	@Override
	public void updateDepartment(CrmDepartment department) {

		htl.update(department);

	}

	/*
	*(non-Javadoc)
	* @see com.qf.dao.CrmDepartmentDao#findbyID(java.lang.Long)
	*/

	@Override
	public CrmDepartment findbyID(Long depId) {

		//TODO
		return htl.get(CrmDepartment.class, depId);

	}

	/*
	*(non-Javadoc)
	* @see com.qf.dao.CrmDepartmentDao#findAllDepartment()
	*/

	@Override
	public List<CrmDepartment> findAllDepartment() {

		//TODO
		return htl.getSessionFactory().getCurrentSession().createQuery("from CrmDepartment",CrmDepartment.class).getResultList();

	}

	
	/*
	*(non-Javadoc)
	* @see com.qf.dao.CrmDepartmentDao#findDepartmentByPage(int, int)
	*/
		
	@Override
	public PageBean findDepartmentByPage(int page, int size) {
		List<CrmDepartment> departments = htl.getSessionFactory().getCurrentSession().createQuery("from CrmDepartment",CrmDepartment.class).setFirstResult((page-1)*size).setMaxResults(size).getResultList();
		int count = htl.getSessionFactory().getCurrentSession().createQuery("from CrmDepartment",CrmDepartment.class).getResultList().size();
		int countPage=(count-1)/size+1;
		return new PageBean(page,size,countPage,departments);
			
	}

	
	/*
	*(non-Javadoc)
	* @see com.qf.dao.CrmDepartmentDao#findByName(java.lang.String)
	*/
		
	@Override
	public CrmDepartment findByName(String name) {
		
		//TODO
		List<CrmDepartment> list = htl.getSessionFactory().getCurrentSession().createQuery("from CrmDepartment as c where c.depName=:name",CrmDepartment.class).setParameter("name", name).getResultList();
		return list.size()>0?list.get(0):null;
	}

}

	